using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;


namespace BAJ_Guide_Drive_System
{
    class Program
    {
        static void Main(string[] args)
        {
            //Instantiate the communications
            //port with some basic settings
            SerialPort comPort = new SerialPort("COM1", 57600, Parity.None, 8, StopBits.One);
            //ConsoleKeyInfo input = Console.ReadKey();

            //Direction and speed
            //int directionLeft = 127;
            //int directionRight = 127;
            //int speedLeft = 255;
            //int speedRight = 255;

            //You have to declare Bytes outside
            //There names are self explainitory
            Byte[] startBAJGuide = { 128 };
            Byte[] setBitRate = { 129 };
            Byte[] bitRate = { 10 };
            Byte[] mode = { 132 };
            Byte[] setDemo = { 136 };
            Byte[] demoCover = { 0 };
            Byte[] demoCoverAndDock = { 1 };
            Byte[] demoSpotCover = { 2 };
            Byte[] demoMouse = { 3 };
            Byte[] demoDriveFigureEight = { 4 };
            Byte[] demoWimp = { 5 };
            Byte[] demoHome = { 6 };
            Byte[] driveStart = { 145 };
            Byte[] moveVelocity = new Byte [4];
            Byte[] moveRadius = new Byte [4];
            Byte[] ending = { 150 };
            Byte[] pause = { 0 };
            bool done = false;
            bool direction = true;

            //Declare the array
            
            

            //Open the port for communications
            if (!comPort.IsOpen)
            {
                comPort.Open();
            }

            //Set things up
            comPort.Write(startBAJGuide, 0, startBAJGuide.Length);
            comPort.Write(setBitRate, 0, setBitRate.Length);
            comPort.Write(bitRate, 0, bitRate.Length);
            comPort.Write(mode, 0, mode.Length);
            
            do
            {   
                ConsoleKeyInfo input = Console.ReadKey();
                if(input.Key == ConsoleKey.UpArrow)
                {
                    comPort.Write(new byte[] { 145, 127, 255, 127, 255}, 0, 5);
                    direction = true;
                    Console.WriteLine("Up");
                }
                if(input.Key == ConsoleKey.DownArrow)
                {
                    comPort.Write(new byte[] { 145, 128, 200, 128, 200 }, 0, 5);
                    direction = false;
                    Console.WriteLine("Down");
                }
                if(input.Key == ConsoleKey.RightArrow && direction == true)
                {
                    comPort.Write(new byte[] { 145, 0, 250, 50, 255 }, 0, 5);
                    Console.WriteLine("Right");
                }
                if(input.Key == ConsoleKey.LeftArrow && direction == true)
                {
                    comPort.Write(new byte[] { 145, 50, 255, 0, 255 }, 0, 5);
                    Console.WriteLine("Left");
                }
                if (input.Key == ConsoleKey.RightArrow && direction == false)
                {
                    comPort.Write(new byte[] { 145, 128, 250, 150, 255 }, 0, 5);
                    Console.WriteLine("Right");
                }
                if (input.Key == ConsoleKey.LeftArrow && direction == false)
                {
                    comPort.Write(new byte[] { 145, 150, 255, 128, 255 }, 0, 5);
                    Console.WriteLine("Left");
                }
                if (input.Key == ConsoleKey.Spacebar)
                {
                    comPort.Write(new byte[] { 145, 0, 0, 0, 0 }, 0, 5);
                    Console.WriteLine("Stop");
                }
                if (input.Key == ConsoleKey.Escape)
                {
                    comPort.Write(pause, 0, pause.Length);
                    done = true;
                }
                
                //comPort.Write(new byte[] { 145, Convert.ToByte(directionRight), Convert.ToByte(speedRight), Convert.ToByte(directionLeft), Convert.ToByte(speedLeft) }, 0, 5);

            } while (!done);

            //Sending Commands
            comPort.Write(startBAJGuide, 0, startBAJGuide.Length);
            comPort.Write(setBitRate, 0, setBitRate.Length);
            comPort.Write(bitRate, 0, bitRate.Length);
            comPort.Write(mode, 0, mode.Length);
            comPort.Write(driveStart, 0, driveStart.Length);
            comPort.Write(new byte[] { 0, 0, 0, 0, 0 }, 0, 5);

            //Close the port
            comPort.Close();
        }
    }
}
